<template>
  <div>
    <div class="filter-container">

      <div class="filter-item" style="width: 100%">
        <el-button style="float: right" type="primary" size="mini" icon="el-icon-download" @click="handleExport">导出</el-button>
      </div>

    </div>

    <el-table
      ref="table"
      :data="tableData"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      :header-cell-style="{'background':'#f2f3f4', 'color':'#555', 'font-weight':'bold', 'line-height':'32px'}"
      row-key="id"
      border
      empty-text="暂无相关数据！"
      default-expand-all
    >
      <el-table-column label="部门名称" prop="title" />
      <el-table-column label="应考人数" prop="totalUser" align="center" />
      <el-table-column label="实考人数" prop="actualUser" align="center" />
      <el-table-column label="通过人数" prop="passUser" align="center" />
      <el-table-column label="通过率" align="center">
        <template scope="scope">
          {{ scope.row.passRate }}%
        </template>
      </el-table-column>

    </el-table>

  </div>
</template>

<script>
import { apiStatDepartTree, apiStatDepartExport } from '@/api/stat/exam'

export default {
  name: 'StatDepart',
  props: {
    examId: String
  }, data() {
    return {
      examIds: [],
      tableData: [],
      listLoading: true,
      listQuery: {
        examId: ''
      }
    }
  },
  created() {
    this.listQuery.examId = this.examId
    this.getList()
  },
  methods: {

    statDetail(id) {
      console.log(id)
      console.log(this.listQuery.examId)
      this.$router.push({ name: 'StatDepartDetail', params: { examId: this.listQuery.examId, departId: id }})
    },

    getList() {
      if (!this.listQuery.examId) {
        return
      }

      this.listLoading = true
      apiStatDepartTree(this.listQuery).then(response => {
        this.tableData = response.data
        this.listLoading = false
      })
    },

    handleExport() {
      if (!this.listQuery.examId) {
        this.$message({
          type: 'error',
          message: '必须先选择一个考试才能导出!'
        })

        return
      }

      // 导出当前查询的数据
      apiStatDepartExport(this.listQuery)
    }
  }
}
</script>
